<?php


namespace app\api\model;

use app\common\model\Reservation as ReservationModel;
use app\common\model\UserTask as UserTaskModel;

class UserTask extends UserTaskModel
{
    public function FinishWork($data){
        $reservation = ReservationModel::detail(['id'=>$data['reservation_id']]);
        if (!$reservation){
            $this->error = '该课程还未下课';
            return false;
        }
        if($reservation['work_status'] == 0){
            $this->error = '该课程还未发布作业';
            return false;
        }
        $task = static::detail(['reservation_id'=>$data['reservation_id']]);
        if ($task){
            $this->error = '已经上传过作业了';
            return false;
        }
        if ($data['file_id'] == ''){
            $this->error = '请先上传视频';
            return false;
        }
        return $this->transaction(function ()use ($data,$reservation){
            $save['user_id'] = $reservation['user_id'];
            $save['coach_id'] = $reservation['coach_id'];
            $save['course_id'] = $reservation['course_id'];
            $save['reservation_id'] = $data['reservation_id'];
            $save['task'] = $reservation['work_title'];
            $save['shop_id'] = $reservation['shop_id'];//门店id
            $save['file_id'] = $data['file_id'];
            $save['image_id'] = $data['image_id'];
            $save['wxapp_id'] = $reservation['wxapp_id'];
            $this->allowField(true)->save($save);
            $userModel = \app\common\model\User::detail(['user_id'=>$reservation['user_id']]);
            $userModel->setIncPoints(10,'发布作业赠送积分');
            return true;
        });

    }
}